package com.yearns.kafka.rabbitmq;

import com.yearns.kafka.entity.User;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author yearns
 * @date 2019/6/28 12:58
 */
@Component
public class RabbitMQSender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void sendDirectQueue() {
        User user = new User();
        user.setUserId("123456");
        user.setName("yearns");
        this.rabbitTemplate.convertAndSend(RabbitMQConfig.QUEUE,user);
    }

    public void sendTopic() {
        User user1 = new User();
        user1.setUserId("123456");
        user1.setName("lizhencheng");

        User user2 = new User();
        user2.setUserId("456789");
        user2.setName("张三");
        this.rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "lzc.message", user1 );
        this.rabbitTemplate.convertAndSend(RabbitMQConfig.TOPIC_EXCHANGE, "lzc.lzc", user2);

    }
    public void sendFanout() {
        User user = new User();
        user.setUserId("123456");
        user.setName("lizhencheng");

        /**
         * 注意， 这里的第2个参数为空。
         * 因为fanout 交换器不处理路由键，只是简单的将队列绑定到交换器上，
         * 每个发送到交换器的消息都会被转发到与该交换器绑定的所有队列上
         */

        this.rabbitTemplate.convertAndSend(RabbitMQConfig.FANOUT_EXCHANGE, "", user );
    }


}
